<template>
    <div class="forget">
        <el-card class="box-card">
            <h2>忘记密码</h2>
            <div v-if="bo1">
                <p class="text">
                请填写你要找回的账号,将发送验证码在您的邮箱
                </p>
                <p>
                    <el-input prefix-icon="el-icon-user" v-model="userName" placeholder="请输入用户名"></el-input>
                    <el-input prefix-icon="el-icon-message" v-model="email" placeholder="请输入邮箱地址"></el-input>
                </p>
                <p>
                    <el-button type="primary" @click="next">下一步</el-button>
                </p>
            </div>
            <div v-if="bo2">
                <p>
                    <el-input prefix-icon="el-icon-user" v-model="userName" placeholder="请输入用户名"></el-input>
                    <el-input prefix-icon="el-icon-unlock" v-model="newPwd" placeholder="请输入新密码"></el-input>
                    <el-input prefix-icon="el-icon-lock" v-model="confirmPwd" placeholder="请确认密码"></el-input>
                    <el-input prefix-icon="el-icon-lock" v-model="vcode" placeholder="请确认收到的验证码"></el-input>
                </p>
                <p>
                    <el-button type="primary" @click="submit">确认修改</el-button>
                </p>
            </div>
            
            
        </el-card>
    </div>
</template>

<script>
export default {
    name:"Forget",
    
    data(){
        return {
            userName : '',
            email : '',
            newPwd : '',
            confirmPwd : '',
            vcode : '',
            bo1 : true,
            bo2 : false
        }
    },
    methods:{
        next(){
            if(this.userName.length == 0){
                this.$message({
                    duration:1500,
                    message: '请输入用户名',
                    type: 'error'
                });
            }else if(this.email.length == 0){
                this.$message({
                    duration:1500,
                    message: '请输入邮箱地址',
                    type: 'error'
                });
            }else{
                this.$axios.post(this.$api.register.Forget,{
                    userName : this.userName,
                    email : this.email
                })
                .then((res)=>{
                    let result = res.data;
                    if(result.code == 200){
                        this.bo1 = false;
                        this.bo2 = true
                    }else{
                        this.$message({
                            duration:1500,
                            message: result.msg,
                            type: 'error'
                        });
                    }
                })
                .catch((err)=>{
                    console.log(err);
                    this.$message({
                        duration:1500,
                        message: '请求出错',
                        type: 'error'
                    });
                })
            }
        },
        submit(){
            if(this.newPwd.length==0){
                this.$message({
                    duration:1500,
                    message: '请输入密码',
                    type: 'error'
                });
            }else if(this.confirmPwd.length==0){
                this.$message({
                    duration:1500,
                    message: '请确认密码',
                    type: 'error'
                });
            }else{
                if(this.newPwd == this.confirmPwd){
                    this.$axios.post(this.$api.register.Check,{
                        userName : this.userName,
                        password : this.newPwd,
                        userCode : this.vcode
                    })
                    .then((res)=>{
                        let result = res.data;
                        if(result.code == 200){
                            this.$message({
                                duration:1500,
                                message: result.msg,
                                type: 'success'
                            });
                            this.$router.push('login')
                        }else{
                            this.$message({
                                duration:1500,
                                message: result.msg,
                                type: 'error'
                            });
                        }
                    })
                    .catch((err)=>{
                        console.log(err)
                        this.$message({
                            duration:1500,
                            message: '请求出错',
                            type: 'error'
                        });
                    })
                }else{
                    this.$message({
                        duration:1500,
                        message: '两次密码不匹配',
                        type: 'error'
                    });
                }
            }
        }
    }
}
</script>

<style>
.text{
    color: rgb(126, 126, 126);
    font-size: 13px;
    text-align: left;
    margin-top: 8px;
}
h2{
    font-size: 30px;
    margin-top: 15px;
}
 .forget{
    padding-top: 100px;
     box-sizing: border-box;
     width: 100%;
     height: 100%;
     background-image: linear-gradient(
    45deg
    , rgba(194, 233, 221, 0.5) 1%, rgba(104, 119, 132, 0.5) 100%), linear-gradient(
    -45deg
    , #494d71 0%, rgba(217, 230, 185, 0.5) 80%);

 }
    .el-input {
        margin: 15px 0;
    }
    .el-button{
        margin-top: 15px;
    }

  .box-card {
     width: 450px;
     /* height: 400px; */
     margin: 0 auto;
  }
  .el-button{
      width: 100% !important;
      height: 45px;
  }
</style>